<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>  
<html>  
    <head>  
    <meta charset="utf-8">  
    <title>数据源配置</title>
	    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/default/easyui.css" />
	    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/themes/icon.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/ainit.css" />
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.json-2.4.min.js"></script>
   		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.easyui.min.1.2.2.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/locale/easyui-lang-zh_CN.js"></script>
	 	<script type="text/javascript">
	 		var a = document.documentElement.clientHeight*0.95;	
	 		var b = document.documentElement.clientWidth*0.95;
	 		//RootTable
	 		var excTables = {};
	 		//ChildredTable
	 		var cexcTables = {};
	 		//DataSourceType
	 		var dataSourceType = {};
	 		//组id
	 		var groupId;
	 		//父级id
	 		var superTableId;
	 		//状态
	 		var state;
	 		//添加的节点
	 		var addSuperTableId;
	 		$(function(){
				
	 			addSuperTableId = "";
	 			groupId = "";
	 			superTableId = "";
	 			state = 0;
	 		 	a = document.documentElement.clientHeight*0.94;
	 		 	b = document.documentElement.clientWidth*0.96;
	 			$('#mtc').window({  
	 							title:'维护列枚举',
							    width:document.documentElement.clientWidth,  
							    height:document.documentElement.clientHeight,  
							    draggable:false,
							    modal:true,
							    resizable:false
							});  
				$('#mtc').window('close');  //关闭窗口 
	 			$.ajax({
	 				url:'${pageContext.request.contextPath}/edi/loadExchangeG',
	 				type:'post',
	 				data:{},
	 				success:function(data){
	 					$('#cc').append("<option value='00' >==请选择==</option>");
	 					for(var i = 0; i < data.length; i++){
	 						$('#cc').append("<option value="+data[i].groupId+">"+data[i].groupName+"</option>");
	 					}
	 				}
	 			});
	 			//界面样式
	 			$('#kk').css('height',a);
	 			$('#kk').css('width',b);
	 			$('#excTD').css('height',a-60);
	 			$('#excTD').css('width',280);
	 			$("input:text").each(function(){
					$(this).attr("class",'ainit-input');
				});
				$("select").each(function(){
					$(this).attr("class",'ainit-select');
				});
				//加载数据源类型
	 			$.ajax({
	 				url:'${pageContext.request.contextPath}/edi/loadDataSourceType',
	 				type:'post',
	 				data:{},
	 				success:function(data){
	 					$('select[name=dataSourceType]').append("<option value='00' >==请选择==</option>");
	 					for(var i = 0; i < data.length; i++){
	 						$('select[name=dataSourceType]').append("<option value="+data[i].code+">"+data[i].typeCname+"</option>");
	 					}
	 				}
	 			});
	 		});
	 		
	 		function changDataSource(obj){
		 		state = 0;
		 		var rtrees = new Array();
		 		var trees = new Array();
	 			groupId = $(obj).val();
	 			if(groupId == '00'){$('#excTableTree').html('请选择数据分组');return;};
	 			$.ajax({
	 				url:'${pageContext.request.contextPath}/edi/loadExchangeTable',
	 				type:'post',
	 				data:{'groupId':groupId},
	 				success:function(data){
	 					excTables = data[0];//将数据保留
	 					cexcTables = data[1];//将取出的数据保留
	 					
	 					var rootTree = {};
	 					rootTree.text =  $('#cc option:selected').text();
	 					rootTree.id = '00';
		 					for(var i = 0; i < data[0].length; i++){
					 			var tree = {};
		 						tree.id = data[0][i].groupTableId;
		 						tree.text = data[0][i].tableName;
		 						tree.children = formatChildren(data[0][i].groupTableId,data[1]);
								if(tree.children != ''){
		 							tree.state = "closed"; 
		 						}
		 						trees[i] = tree;
		 					};
		 				rootTree.children = trees;
		 				rtrees[0] = rootTree;
		 				$('#excTableTree').tree({
		 					animate:true,
							data: rtrees,
							onClick: function(node){
								//alert(node.id);  // alert node text property when clicked
								loadExchangeTableInfo(node.id);
							}
						});	
	 				}
	 			});
	 		}
	 		
	 		function  formatChildren(groupTableId,list){
	 			var tmpTreeArr = new Array();
	 			var chiL = 0;
	 			for (var i = 0; i < list.length; i++){
	 				if(list[i].superTableId == groupTableId){
		 				var tmpTree = {};
		 				tmpTree.id = list[i].groupTableId;
		 				tmpTree.text = list[i].tableName;
		 				tmpTree.children = formatChildren(list[i].groupTableId,list);
		 				//if(tmpTree.children != ''){
			 				//tmpTree.state = "closed"; 
		 				//}
		 				tmpTreeArr[chiL++] = tmpTree;
	 				}
	 			}
	 			return tmpTreeArr;
	 		}
	 		
	 		function loadExchangeTableInfo(tableId){
	 			if(tableId == '00'){
	 				$('input[name=groupTableId]').val('');
	 				$('input[name=tableName]').val('');
	 				$('select[name=dataSourceType]').attr('value','00');
	 				return; 
	 			}
	 			for(var i = 0; i<excTables.length; i++){
	 				if(excTables[i].groupTableId == tableId){
	 					fillInput(excTables[i]);
	 					return;
	 				}
	 			}
	 			for(var i =0; i< cexcTables.length; i++){
	 				if(cexcTables[i].groupTableId == tableId){
	 					fillInput(cexcTables[i]);
	 					return;
	 				}
	 			}
	 			return;
	 		}
	 		
	 		function fillInput(excTable){
	 			if(state == 1){
					var node = $('#excTableTree').tree('find','');
					$('#excTableTree').tree('remove',node.target);
					state = 0;
				}
	 			if(excTable.groupTableId == ''){
	 				$("input[name=groupTableId]").attr("class",'ainit-input-err');
	 				return;
	 			}
	 			$('input[name=groupTableId]').val(excTable.groupTableId);
	 			$('input[name=tableName]').val(excTable.tableName);
	 			$('select[name=dataSourceType]').attr('value',excTable.dataSourceType);
	 			$('input[name=xmlLabelName]').val(excTable.xmlLabelName);
	 			$('input[name=displayXmlSuperLabel]').val(excTable.displayXmlSuperLabel);
	 			$('input[name=dataSelectDsid]').val(excTable.dataSelectDsid);
	 			$('input[name=sortNum]').val(excTable.sortNum);
	 			$('input[name=incrMode]').val(excTable.incrMode);
	 			superTableId = excTable.superTableId;
	 		}
	 		
	 		function addExcTable(){
	 			 if(checkForm() == 1) return;
	 			 if(state == 1){
				 	superTableId = addSuperTableId;
				 }
	 			 $.ajax({
	 			 	url:"${pageContext.request.contextPath}/edi/saveExchangeTable",
	 			 	type:'post',
	 			 	data:$('#tableForm').serialize()+"&groupId="+groupId+"&superTableId="+superTableId,
	 			 	success:function(data){
	 			 		if(data.msg == 'upSuccess'){
	 			 			$.messager.alert('提示框','更新成功');
	 			 		}else if(data.msg == 'Success'){
	 			 			$('input[name=groupTableId]').val(data.info);
	 			 			$.messager.alert('提示框','保存成功');
	 			 		}else{
	 			 			$.messager.alert('错误提示框',data.msg,'error');
	 			 		}
	 			 		changDataSource($('#cc'));
	 			 	}
	 			 });
	 			 state = 0;
	 		}
	 		
	 		function checkForm(){
	 			if($('input[name=tableName]').val().trim() == ''){
	 				$('input[name=tableName]').focus();
	 				return 1;
	 			}
	 			if($('select[name=dataSourceType]').val() == '00'){
	 				$.messager.alert('提示框','请选择数据源类型','info');
	 				return 1;
	 			}
	 			return;
	 		}
	 		
	 		function resetForm(){
	 			if(state == 1){
					var node = $('#excTableTree').tree('find','');
					$('#excTableTree').tree('remove',node.target);
				}
	 			var selected = $('#excTableTree').tree('getSelected');
	 			if(groupId == '' || groupId == '00' || selected == null || selected == ''){
		 			$.messager.alert('提示框','请选择数据分组','info');
		 			state = 0;
		 			return;
	 			}
	 			addSuperTableId = $('input[name=groupTableId]').val();
	 			$('input[name=groupTableId]').val('');
	 			$('input[name=tableName]').val('');
	 			$('select[name=dataSourceType]').attr('value','00');
	 			superTableId = '';
	 			selected.target.state = 'open';
				$('#excTableTree').tree('append', {
					parent: selected.target,
					data: [{
						id: '',
						text: ' '
					}]
				});
	 			if (selected) {
                   if(selected.children != ''){
			 				$("#excTableTree").tree('expand',selected.target);
		 			}
                }
	 			$('input[name=tableName]').focus();
	 			state = 1;
	 		}
	 		//删除数据分组
	 		function delExcTable(){
	 			var node = $('#excTableTree').tree('getSelected');
                var b = $('#excTableTree').tree('isLeaf', node.target);
                if(b){
					$.messager.confirm('提示', '是否删除选中节点', function(btn2){
				        //点击确定则删除,否则取消
					        if (btn2) {
					            $.ajax({
					 			 	url:"${pageContext.request.contextPath}/edi/delExchangeTable",
					 			 	type:'post',
					 			 	data:{'groupTableId':node.id},
					 			 	success:function(data){
					 			 		if(data.msg == 'Success'){
					 			 			 $('#excTableTree').tree('remove',node.target);
					 			 		}else{
					 			 			$.messager.alert('错误提示框',data.msg,'error');
					 			 		}
					 			 		//changDataSource($('#cc'));
					 			 	}
					 			 });
					        }
					});                	
                }else{
                	$.messager.alert('提示框','请先删除叶子节点','info');
                }
	 		}
	 		
	 		//维护列枚举
	 		function maintainColn(){
	 			if(state == 1){
					var node = $('#excTableTree').tree('find','');
					$('#excTableTree').tree('remove',node.target);
				}
	 			var selected = $('#excTableTree').tree('getSelected');
	 			if(selected == null || selected == '' || selected.id == '00'){
		 			$.messager.alert('提示框','请选择数据分组','info');
		 			state = 0;
		 			return;
	 			}
	 			if($('#maintainC').html() == ''){
		 			$('#maintainC').load(  
					    '${pageContext.request.contextPath}/edi/maintainCln',  
					    {groupTableId:$('input[name=groupTableId]').val(),groupId:groupId}
					);
	 			}else{
	 				var tdata = {};
	 				tdata.rows = new Array();
	 				$('#maintainClnTable').datagrid('loadData',tdata);
	 				$.ajax({
	 					url:'${pageContext.request.contextPath}/edi/queryExcClnByIds',
	 					data:{groupTableId:$('input[name=groupTableId]').val(),groupId:groupId},
	 					success:function(data){
			 				$('#maintainClnTable').datagrid('loadData',data);
	 					}
	 				});
	 			}
	 			$('#mtc').window('open');
	 		}
	 	</script>
    </head>
    <body>  
    	<table id="kk">
    		<tr>
    			<td colspan="2" style="height: 30px;">
    				数据分组：<select onchange="changDataSource(this)" id="cc" class="ainit-combobox"></select> 
    				<a href="#" class="easyui-linkbutton" onclick="javascript:maintainColn()">维护列枚举</a>  </td>
    		</tr>
    		<tr>
    			<td  id='excTD' ><div id='excTableTree' style="overflow:auto;width:100%;height:100%; border:1px #8DB2E3 solid;"></div></td>
    			<td style="vertical-align: top; padding-left: 20px;" >
    				<div id='excTableInfo' >
						<form id="tableForm" method='post'>
							<table>
								<tr>
									<td class='ainit-tname-require'>交换项编号:</td><td><input readonly="readonly" name='groupTableId'/></td>
									<td class='ainit-tname-require'>交换项名称:</td><td><input name='tableName'/></td>
								</tr>
								<tr>
									<td class='ainit-tname-require'>数据源类型:</td><td><select name='dataSourceType'></select></td>
									<td class='ainit-tname-require'>xml标签名:</td><td><input name='xmlLabelName'/></td>
								</tr>
								<tr>
									<td>xml行集标签名:</td><td><input name='displayXmlSuperLabel'/></td>
									<td>数据选择DSID:</td><td><input name='dataSelectDsid'/></td>
								</tr>
								<tr>
									<td>排序号:</td><td><input name='sortNum'/></td>
									<td>增量方式:</td><td><input name='incrMode'/></td>
								</tr>
							</table>
						</form>
					</div>
				</td>
    		</tr>
    		<tr>
    			<td colspan="2"  style="height: 30px;" align="center">
					<a href="#" id='save' class="easyui-linkbutton" onclick="javascript:addExcTable()">保存</a> 
		    		<a href="#" id='add' class="easyui-linkbutton" onclick="javascript:resetForm()">添加</a> 
		    		<a href="#" id='add' class="easyui-linkbutton" onclick="javascript:delExcTable()">删除</a> 
				</td>
    		</tr>
    	</table>
    	<div id='mtc'>
    		<div id='maintainC'></div>
    	</div>
	</body>  
</html>  